//
//  MVRedPacketOrderContainerController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/4.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHRedPacketOrderContainerController.h"
#import "MHRedPacketOrderRecordViewController.h"
#import "MHRedPacketOrderViewModel.h"
#import "MHRedPacketHeaderView.h"
#import "MHRedPacketHistoryHeader.h"
#import "MHRedHistorySummary.h"
@interface MHRedPacketOrderContainerController ()
@property(nonatomic, strong)  MHRedPacketHistoryHeader *leftHeaderView;
@property(nonatomic, strong)  MHRedPacketHistoryHeader *rightHeaderView;

@property(nonatomic, strong) MHRedHistorySummary *summary;
@end

@implementation MHRedPacketOrderContainerController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.titles == nil) {
        _titles = @[ @"抢到的红包",@"我发的红包",];
    }
    
    [super viewDidLoad];
    [self setupHeadr];
    
    
    
//    mh_
}

-(void) setupHeadr
{
    self.myCategoryView.averageCellSpacingEnabled = YES;
    self.myCategoryView.cellSpacing =  0;
    self.myCategoryView.titleFont = MHRegularFont_13;
    self.myCategoryView.titles = self.titles;
    self.myCategoryView.titleColor = MHMetionTextColor;
    self.myCategoryView.titleSelectedColor = MHTextColor333;
    
    [self.view addSubview:self.myCategoryView];
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorLineWidth = MH_SCREEN_WIDTH/2;
    lineView.lineStyle = JXCategoryIndicatorLineStyle_JD;
    lineView.indicatorLineViewColor = MHColorFromHexString(@"#FE313D");
    lineView.verticalMargin = 5;
    lineView.height = 2;
    self.myCategoryView.indicators = @[lineView];
    self.myCategoryView.frame = CGRectMake(0, 0, MH_SCREEN_WIDTH, 50);
    UIImageView *splistImage = [UIImageView new];
    splistImage.backgroundColor = MHSpliteLineColor;
    [self.view addSubview:splistImage];
    [splistImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.equalTo(@1);
//        make.top.equalTo(self.categoryView.mas_bottom);
        make.centerY.equalTo(lineView.mas_centerY);
    }];
    
   MHRedPacketHistoryHeader *leftHeaderView = [MHRedPacketHistoryHeader mh_viewFromXib];
    [self.view addSubview:leftHeaderView];
    
    [leftHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.width.equalTo(@(MH_SCREEN_WIDTH/2));
        make.height.equalTo(@106);
        make.top.equalTo(splistImage.mas_bottom);
    }];
    
    self.leftHeaderView = leftHeaderView;
    
    
    MHRedPacketHistoryHeader *rightHeaderView = [MHRedPacketHistoryHeader mh_viewFromXib];
    [self.view addSubview:rightHeaderView];
    rightHeaderView.iconImageView.image  = [UIImage imageNamed:@"ic_bill_package2"];
   
    [rightHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftHeaderView.mas_right);
        make.right.equalTo(self.view);
        make.height.equalTo(@106);
        make.top.equalTo(leftHeaderView);
    }];
    
    self.rightHeaderView = rightHeaderView;
    
    
   
    [self requeSummeryDataWithInComme:YES];
    

}

-(void) requeSummeryDataWithInComme:(BOOL) isIcome
{
    @weakify(self);
    [[[[MHHTTPService sharedInstance] getUserRedHistorySummary ] mh_parsedResults] subscribeNext:^(MHRedHistorySummary *summery) {
        @strongify(self);
        self.summary = summery;
        [self configHeaderWithIncomme:isIcome];
    }];
}

-(CGRect)scrollFrame
{
    CGFloat headerHegith =  self.myCategoryView.height + 106;
    return CGRectMake(0, headerHegith, MH_SCREEN_WIDTH, MH_SCREEN_HEIGHT - headerHegith - MH_APPLICATION_TOP_BAR_HEIGHT);
}

-(UIViewController *)preferredCategoryViewControllerWithIndex:(int)index
{
    if (index == 0) {
        MHRedPacketOrderViewModel *orderViewModel =   [[MHRedPacketOrderViewModel alloc] initWithServices:self.viewModel.services params:@{MHViewModelIDKey:@(RedPacketHistoryTypeReceive)}];
        MHRedPacketOrderRecordViewController *nearByVc= [[MHRedPacketOrderRecordViewController alloc] initWithViewModel:orderViewModel];
        return nearByVc;
        
    }else
    {
        
        MHRedPacketOrderViewModel *orderViewModel =   [[MHRedPacketOrderViewModel alloc] initWithServices:self.viewModel.services params:@{MHViewModelIDKey:@(RedPacketHistoryTypeSend)}];
        MHRedPacketOrderRecordViewController *nearByVc= [[MHRedPacketOrderRecordViewController alloc] initWithViewModel:orderViewModel];
        return nearByVc;
//        MHDiscoverHomeViewController *discover = [[MHDiscoverHomeViewController alloc] initWithViewModel:  [[MHDiscoverHomeViewModel alloc] initWithServices:self.viewModel.services params:@{@"MHViewModelShowLocation": @(NO)}]];
//        return discover;
    }
}

- (CGFloat)preferredCategoryViewHeight {
    return 0.01;
}


- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    BOOL isInCome = (index == 0);
    if (self.summary) {
        [self configHeaderWithIncomme:isInCome];
    }else
    {
        [self requeSummeryDataWithInComme:isInCome];
    }

}

-(void)configHeaderWithIncomme:(BOOL) isInCome
{
    _leftHeaderView.tipLabel.text = isInCome ?  @"收到总金额" : @"发布总金额";
    _rightHeaderView.tipLabel.text = isInCome ?  @"当日抢到金额": @"当日发布金额";
    _leftHeaderView.amountLabel.text = [NSString stringWithFormat:@"%.2f",isInCome ? self.summary.totalReceived: self.summary.totalLose ];
    _rightHeaderView.amountLabel.text = [NSString stringWithFormat:@"%.2f",isInCome ? _summary.todayReceived: _summary.todayLose];
}


- (JXCategoryTitleView *)myCategoryView {
    return (JXCategoryTitleView *)self.categoryView;
}

- (NSUInteger)preferredListViewCount {
    return self.titles.count;
}

- (Class)preferredCategoryViewClass {
    return [JXCategoryTitleView class];
}

@end
